package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.DateTimePickDialogUtil;
import com.bocai.eelly.utils.DialogUtil;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.TimeUtils;
import com.bocai.eelly.utils.Utils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 发布新订单
 */
public class PostOrderActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rb_post_order_tax_yes)
    RadioButton rbPostOrderTaxYes;
    @Bind(R.id.rb_post_order_tax_no)
    RadioButton rbPostOrderTaxNo;
    @Bind(R.id.rg_post_order_tax)
    RadioGroup rgPostOrderTax;
    @Bind(R.id.et_post_order_style_name)
    EditText etPostOrderStyleName;
    @Bind(R.id.tv_post_order_fabric_type)
    TextView tvPostOrderFabricType;
    @Bind(R.id.ll_post_order_fabric_type)
    LinearLayout llPostOrderFabricType;
    @Bind(R.id.et_post_order_fabric_name)
    EditText etPostOrderFabricName;
    @Bind(R.id.et_post_order_num)
    EditText etPostOrderNum;
    @Bind(R.id.tv_post_order_start_time)
    TextView tvPostOrderStartTime;
    @Bind(R.id.ll_post_order_start_time)
    LinearLayout llPostOrderStartTime;
    @Bind(R.id.et_post_order_price)
    EditText etPostOrderPrice;
    @Bind(R.id.tv_post_order_handle_time)
    TextView tvPostOrderHandleTime;
    @Bind(R.id.ll_post_order_handle_time)
    LinearLayout llPostOrderHandleTime;
    @Bind(R.id.tv_post_order_require)
    TextView tvPostOrderRequire;
    @Bind(R.id.ll_post_order_require)
    LinearLayout llPostOrderRequire;
    @Bind(R.id.et_post_order_to_country)
    EditText etPostOrderToCountry;
    @Bind(R.id.tv_post_order_procedure)
    TextView tvPostOrderProcedure;
    @Bind(R.id.ll_post_order_procedure)
    LinearLayout llPostOrderProcedure;
    @Bind(R.id.et_post_order_backup)
    EditText etPostOrderBackup;
    @Bind(R.id.et_post_order_contacts)
    EditText etPostOrderContacts;
    @Bind(R.id.et_post_order_tel)
    EditText etPostOrderTel;
    @Bind(R.id.ll_post_order_choose_photo)
    LinearLayout llPostOrderChoosePhoto;
    @Bind(R.id.tv_post_order_commit)
    TextView tvPostOrderCommit;
    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;
    private String tax="1";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_order);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_post_order, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }
    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }

    private void initEvent() {
        rbPostOrderTaxYes.setChecked(true);
        rgPostOrderTax.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId)
                {
                    case R.id.rb_post_order_tax_yes:
                        tax="1";
                        break;
                    case R.id.rb_post_order_tax_no:
                        tax="0";
                        break;
                }
            }
        });
        llPostOrderChoosePhoto.setOnClickListener(this);
        llPostOrderFabricType.setOnClickListener(this);
        llPostOrderHandleTime.setOnClickListener(this);
        llPostOrderProcedure.setOnClickListener(this);
        llPostOrderRequire.setOnClickListener(this);
        llPostOrderStartTime.setOnClickListener(this);
        tvPostOrderCommit.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ll_post_order_choose_photo:
                Intent intent=new Intent(this,ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.ll_post_order_fabric_type:
                final List<String> fabricTypes=new ArrayList<>();
                String [] fabricType={"梭织面料","针织面料","皮革皮草","无纺布","毛纺布","其他面料"};
                for (int i=0;i<fabricType.length;i++)
                {
                    fabricTypes.add(fabricType[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, fabricTypes), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostOrderFabricType.setText(fabricTypes.get(position));
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.ll_post_order_start_time:
                new DateTimePickDialogUtil(this).dateTimePicKDialog(tvPostOrderStartTime);
                break;
            case R.id.ll_post_order_handle_time:
                new DateTimePickDialogUtil(this).dateTimePicKDialog(tvPostOrderHandleTime);
                break;
            case R.id.ll_post_order_procedure:
                final List<String> procedures=new ArrayList<>();
                String [] procedure={"裁缝","车缝","后整理","其他"};
                for (int i=0;i<procedure.length;i++)
                {
                    procedures.add(procedure[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, procedures), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostOrderProcedure.setText(procedures.get(position));
                        dialog.dismiss();
                    }
                });
                break;

            case R.id.ll_post_order_require://质量要求
                final List<String> requires=new ArrayList<>();
                String [] require={"高","较高","一般"};
                for (int i=0;i<require.length;i++)
                {
                    requires.add(require[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, requires), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostOrderRequire.setText(requires.get(position));
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_post_order_commit:
                if (files == null || files.length == 0) {
                    T.showShort(this, "请选择照片");
                } else {
                    String style = etPostOrderStyleName.getText().toString();
                    String fabric_type=tvPostOrderFabricType.getText().toString();
                    String fabric_name = etPostOrderFabricName.getText().toString();
                    String num = etPostOrderNum.getText().toString();
                    String start_time = tvPostOrderStartTime.getText().toString();
                    String target_price = etPostOrderPrice.getText().toString();
                    String handle_time = tvPostOrderHandleTime.getText().toString();
                    String requirename = tvPostOrderRequire.getText().toString();
                    String to_country=etPostOrderToCountry.getText().toString();
                    String procedurename = tvPostOrderProcedure.getText().toString();
                    String backup = etPostOrderBackup.getText().toString();
                    String contacts = etPostOrderContacts.getText().toString();
                    String tel = etPostOrderTel.getText().toString();
                    if (Utils.isEmpty(style)) {
                        T.showShort(this, "请输入款式名称");
                    } else if (Utils.isEmpty(fabric_type)) {
                        T.showShort(this, "请选择面料类别");
                    } else if (Utils.isEmpty(fabric_name)) {
                        T.showShort(this, "请输入面料名称");
                    } else if (Utils.isEmpty(num)) {
                        T.showShort(this, "请输入订单数量");
                    } else if (Utils.isEmpty(start_time)) {
                        T.showShort(this, "请选择可上车位日期");
                    } else if (Utils.isEmpty(target_price)) {
                        T.showShort(this, "请输入目标价");
                    } else if (Utils.isEmpty(handle_time)) {
                        T.showShort(this, "请选择交货日期");
                    } else if (Utils.isEmpty(requirename)) {
                        T.showShort(this, "请选择质量要求");
                    } else if (Utils.isEmpty(to_country)) {
                        T.showShort(this, "请输入销往国家");
                    } else if (Utils.isEmpty(procedurename)) {
                        T.showShort(this, "请选择工序要求");
                    } else if (Utils.isEmpty(backup)) {
                        T.showShort(this, "请输入其他说明");
                    }else if (Utils.isEmpty(contacts)) {
                        T.showShort(this, "请输入联系人");
                    } else if (Utils.isEmpty(tel)) {
                        T.showShort(this, "请输入联系电话");
                    }else {
                        Loading.show(this, getString(R.string.str_loading));
                        commit();
                    }
                }

                break;
        }
    }

    private void commit() {
        String startTime = TimeUtils.dataOne(tvPostOrderStartTime.getText().toString());
        String handleTime = TimeUtils.dataOne(tvPostOrderHandleTime.getText().toString());
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POSTORDER, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(PostOrderActivity.this,e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(PostOrderActivity.this, response);
                }
            },files,fileKeys,getParams(etPostOrderStyleName.getText().toString(),tvPostOrderFabricType.getText().toString(),etPostOrderFabricName.getText().toString(),
                    etPostOrderNum.getText().toString(),startTime, etPostOrderPrice.getText().toString(),tax,handleTime,tvPostOrderRequire.getText().toString(),
                    etPostOrderToCountry.getText().toString(),tvPostOrderProcedure.getText().toString(),etPostOrderBackup.getText().toString(),
                    etPostOrderContacts.getText().toString(),etPostOrderTel.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public OkHttpClientManager.Param[] getParams(String style, String fabric_type, String fabric_name, String num, String start_time, String target_price,
                                                 String tax, String handle_time,String require,String to_country,String procedure,String backup,
                                                 String contacts,String tel){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(this,"id")),
                new OkHttpClientManager.Param("style", style),
                new OkHttpClientManager.Param("fabric_type", fabric_type),
                new OkHttpClientManager.Param("fabric_name", fabric_name),
                new OkHttpClientManager.Param("num", num),
                new OkHttpClientManager.Param("start_time", start_time),
                new OkHttpClientManager.Param("target_price", target_price),
                new OkHttpClientManager.Param("tax", tax),
                new OkHttpClientManager.Param("handle_time", handle_time),
                new OkHttpClientManager.Param("require", require),
                new OkHttpClientManager.Param("to_country", to_country),
                new OkHttpClientManager.Param("procedure", procedure),
                new OkHttpClientManager.Param("backup", backup),
                new OkHttpClientManager.Param("contacts", contacts),
                new OkHttpClientManager.Param("tel", tel)
        };
    }
}
